package com.server.web.controller.pinggu;

import com.server.common.annotation.Log;
import com.server.common.core.controller.BaseController;
import com.server.common.core.domain.AjaxResult;
import com.server.common.core.page.PageDomain;
import com.server.common.core.page.TableDataInfo;
import com.server.common.core.page.TableSupport;
import com.server.common.enums.BusinessType;
import com.server.common.utils.StringUtils;
import com.server.pinggu.domain.SenUser;
import com.server.pinggu.domain.SenUserdata;
import com.server.pinggu.service.ISenUserService;
import com.server.pinggu.service.ISenUserdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ltao
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/sen/user")
public class SenUserController extends BaseController
{
    @Autowired
    private ISenUserService senUserService;

    @Autowired
    private ISenUserdataService iSenUserdataService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SenUser senUser)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        TableDataInfo rspData = senUserService.queryList(senUser,pageNum,pageSize);
        return rspData;
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(senUserService.getUserInfoById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SenUser senUser)
    {
        Date date = new Date();
        senUser.setDttime(date.getTime()/1000);
        boolean save = senUserService.save(senUser);
        if (StringUtils.isNotEmpty(senUser.getXm())){
            SenUserdata senUserdata = new SenUserdata();
            senUserdata.setId(senUser.getId());
            senUserdata.setAds("null");
            senUserdata.setRealname(senUser.getXm());
            iSenUserdataService.save(senUserdata);
        }
        return toAjax(save);

    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SenUser senUser)
    {
        boolean b = senUserService.updateById(senUser);
        if (StringUtils.isNotEmpty(senUser.getXm())){
            SenUserdata senUserdata = new SenUserdata();
            senUserdata.setId(senUser.getId());
            senUserdata.setRealname(senUser.getXm());
            iSenUserdataService.updateById(senUserdata);
        }
        return toAjax(b);
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(senUserService.removeByIds(Arrays.asList(ids)));
    }
}
